package com.zgr.struts.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zgr.struts.dao.IUserDao;
import com.zgr.struts.dao.daoimpl.UserDaoImpl;
import com.zgr.struts.entity.User;
import com.zgr.struts.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 控制用户的业务。
 */
public class UserAction extends SuperAction implements ModelDriven<User> {

    private User user = new User();
    /**
     * 登陆业务
     * @return
     */
    public String loginUsername(){
        IUserDao dao = new UserDaoImpl();

        //登陆成功后
        if(dao.login(user)){
            //登陆成功后将username添加到session
            session.setAttribute("username",user.getUsername());
            //查看session是否添加成功
            System.out.println("session:" + session.getAttribute("username"));
            return "login_success";
        }
        return LOGIN;
    }

    public String register(){
        IUserDao dao = new UserDaoImpl();
        dao.register(user);
        return "register_success";
    }

    @Override
    public User getModel() {
        return this.user;
    }
}
